package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.service.CategoryService;
import com.sky.vo.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * PROJECT_NAME: sky-take-out
 * NAME: CategoryServiceImpl
 *
 * @AUTHOR: zhang
 * DATE: 2023/11/22
 * DESCRIPTION :
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;
    /**
     * 分类管理列表查询
     * @param dto
     * @return
     */
    @Override
    public PageResult getPage(CategoryPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<Category> pageResult = categoryMapper.getPage(dto);
        PageResult result = PageResult.builder().total(pageResult.getTotal()).records(pageResult.getResult()).build();
        return result;
    }

    /**
     * 新增
     * @param dto
     */
    @Override
    public void insert(CategoryDTO dto) {
        Category category = new Category();
        BeanUtils.copyProperties(dto, category);
        //补齐字段
        category.setStatus(0);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.insert(category);
    }

    /**
     * 修改分类信息
     * @param dto
     */
    @Override
    public void update(Integer status,CategoryDTO dto) {
        categoryMapper.update(status,dto);
    }

    @Override
    public void delete(Long id) {
        categoryMapper.delete(id);
    }

    @Override
    public List<Category> selectCategoryByType(Integer type) {
        return categoryMapper.selectCategoryByType(type);
    }
}
